{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4906 64\n",
      "4906 11\n",
      "3924 64\n",
      "982 64\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import glob\n",
    "import cv2\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import LabelEncoder    #用于Label编码\n",
    "from sklearn.preprocessing import OneHotEncoder     #用于one-hot编码\n",
    "\n",
    "\n",
    "#图片转成数组形式\n",
    "datas = []\n",
    "labels = []\n",
    "filepaths = glob.glob(\"faceImageGray/*\")\n",
    "for filepath in filepaths:\n",
    "    files = glob.glob(filepath + \"/\" + '*.jpg') \n",
    "    for file in files:\n",
    "        img = cv2.imread(file)\n",
    "        binary_img = np.array(img)\n",
    "        datas.append(binary_img)\n",
    "        labels.append(filepath.split('\\\\')[-1])\n",
    "\n",
    "data = np.array(datas)\n",
    "label = np.array(labels)\n",
    "data_label = LabelEncoder().fit_transform(label)\n",
    "of=OneHotEncoder(sparse=False).fit(data_label.reshape(-1,1))  \n",
    "data_ohe=of.transform(data_label.reshape(-1,1))\n",
    "\n",
    "np.save('data.npy',data)\n",
    "np.save('label.npy',data_ohe)\n",
    "\n",
    "Data = np.load('data.npy')\n",
    "Label = np.load('label.npy')\n",
    "train_x,test_x,train_y,test_y = train_test_split(Data,Label,test_size=0.2,random_state=42)\n",
    "print(Data.shape[0],Data.shape[1])\n",
    "print(Label.shape[0],Label.shape[1])\n",
    "print(train_x.shape[0],train_x.shape[1])\n",
    "print(test_x.shape[0],test_x.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
